package com.example.zqq.mysparkview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 在手指点击处会不停的喷射火花粒子
 */
public class SparkActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new SparkView(this));
        TestGson();
    }

    public void TestGson() {
        //转换器
        GsonBuilder builder = new GsonBuilder();
        // 不转换没有 @Expose 注解的字段
        builder.excludeFieldsWithoutExposeAnnotation();
        Gson gson = builder.create();
//        Gson gson = new Gson();

        //1、对象转string
        Student stu = new Student();                //此对象属性 要 加 @Expose(因为上句话)
        stu.setId(333);
        stu.setName("qqq");
        String stuStr = gson.toJson(stu);
        Log.e("Tag1-SparkAct:", stuStr);        //{"studentName":"qqq","studentId":333}

        //2、string转对象
        Student student = gson.fromJson(stuStr, Student.class);
        Log.e("Tag2-SparkAct-toObj:", student.toString());

        String stuTemp = "{\"name\":\"qq2\",\"id\":335}";
        Student student1 = gson.fromJson(stuTemp, Student.class);
        Log.e("Tag3-SparkAct-toObj2:", student1.toString());

        //3、对象List转string
        List<Student> studentList = new ArrayList<>();
        Student student2 = new Student();
        student2.setName("zzz");
        student2.setId(8888);
        studentList.add(student2);
        //Gson gsonList = new Gson();
        Type type = new TypeToken<List<Student>>(){}.getType(); //指定集合对象属性

        String beanListToJson = gson.toJson(studentList, type);
        Log.e("Tag4-SparkAct-list2str:", beanListToJson);        //[{"studentName":"zzz","studentId":8888}]

        //4.集合string转对象list
        List<Student> testBeanListFromJson = gson.fromJson(beanListToJson, type);
        System.out.println(testBeanListFromJson);
        Log.e("Tag5-SparkAct-str2list:", testBeanListFromJson.toString());      //[zzz:8888]


        //5、集合如果不指定类型 默认为String
        List<String> testList = new ArrayList<String>();
        testList.add("first");
        testList.add("second");
        String listToJson = gson.toJson(testList);
        Log.e("Tag6-SparkAct-list2str:", listToJson); //["first","second"]

        //6、集合字符串转回来需要指定类型
        List<String> testList2 = (List<String>) gson.fromJson(listToJson, new TypeToken<List<String>>(){}.getType());
        Log.e("Tag7-SparkAct-str2list:", testList2.get(0));

        /**-6、 将HashMap字符串转换为 JSON --**/
        Map<String, String> testMap = new HashMap<String, String>();
        testMap.put("id", "id.first");
        testMap.put("name", "name.second");
        String mapToJson = gson.toJson(testMap);
        Log.e("Tag8-SparkAct-map2json:", mapToJson);    //{"id":"id.first","name":"name.second"}

        //7、stringMap转对象
        Map<String, String> userMap2 =  gson.fromJson(mapToJson,new TypeToken<Map<String, String>>() {}.getType());
        Log.e("Tag9-SparkAct-str2map:", userMap2.toString());//{id=id.first, name=name.second}

        /**-8、对象含有普通对象、集合、map情况 --*/
        Student user1 = new Student();
        user1.setId(1001);
        user1.setName("张三");

        Student user3 = new Student();
        user3.setId(1002);
        user3.setName("李四");

        Map<String, Student> userMap = new HashMap<>();
        userMap.put("user1", user1);
        userMap.put("user3", user3);

        List<Student> userList = new ArrayList<>();
        userList.add(user1);
        userList.add(user3);

        Teacher groupBean = new Teacher();
        groupBean.setStudent(user1);
        groupBean.setStus(userList);
        groupBean.setMap((HashMap) userMap);

        Gson gsonGroup = new Gson();
        String sGroupBean = gsonGroup.toJson(groupBean, new TypeToken<Teacher>() {}.getType());
        Log.e("Tag8-SparkAct-map2json:", sGroupBean);
        /*
        {"stus":[{"name":"张三","id":1001},{"name":"李四","id":1002}],
        "map":{"user3":{"name":"李四","id":1002},"user1":{"name":"张三","id":1001}},
        "student":{"name":"张三","id":1001},
        "id":0,"age":0}
        */

        //9、复杂对象string转对象 【用的是--这个】
        Teacher groupBean2 = (Teacher) gson.fromJson(sGroupBean,new TypeToken<Teacher>() {}.getType());
        Log.e("Tag9-SparkAct-json2obj:", groupBean2.toString());


    }


    class Student {
        //没有@Expose注释的属性将不会被序列化.。
        //@Expose(serialize = false, deserialize = false)

        @Expose     //serialize 和 deserialize，默认值都为 true
        String name;
        @Expose
        int id;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    '}';
        }
    }

    class Teacher {
        @Expose
        private int id;
        @Expose
        private String name;
        @Expose
        private int age;
        @Expose
        private Student student;
        @Expose
        private List stus;
        @Expose
        private HashMap map;

        @Override
        public String toString() {
            return "{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    ", student=" + student +
                    ", stus=" + stus +
                    ", map=" + map +
                    '}';
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Student getStudent() {
            return student;
        }

        public void setStudent(Student student) {
            this.student = student;
        }

        public List getStus() {
            return stus;
        }

        public void setStus(List stus) {
            this.stus = stus;
        }

        public HashMap getMap() {
            return map;
        }

        public void setMap(HashMap map) {
            this.map = map;
        }
    }

}
